window.onload = function () {
    var vue = new Vue({
        el: "#app",
        data: {
            dataList: [],
            brand: {},
            pages: 0,
            page: 1,
            brandSearch: {},
            ids: [],
            checked: false
        },
        methods: { // 定义操作方法
            search: function (page) { // 搜索方法
                // 发送异步请求
                axios.get("/brand/findByPage?page=" + page,
                    {params: window.brandSearch}).then(function (res) {
                    // 获取响应数据
                    console.log(this);
                    vue.dataList = res.data.rows;
                    vue.pages = res.data.pages;
                    vue.page = page;
                    vue.ids = [];
                });
            },
            saveOrUpdate: function () {
                var url = "/save";
                if (this.brand.id) {
                    url = "/update";
                }
                axios.post("/brand" + url, vue.brand).then(function (value) {
                    if (value.data) {
                        setTimeout('vue.search(vue.pages)',500);
                        vue.search(vue.pages)
                        if(vue.page==vue.pages&&vue.dataList.length==10){
                            vue.search(vue.pages+1)
                        }
                    } else {
                        alert("操作失败");
                    }
                });
            },
            show: function (brand) {
                var jsonStr = JSON.stringify(brand);
                this.brand = JSON.parse(jsonStr);
            },
            clickAll: function (e) {
                this.ids = [];
                if (e.target.checked) {
                    for (var i = 0; i < this.dataList.length; i++) {
                        this.ids.push(this.dataList[i].id);
                    }
                }
            },
            del: function () {
                if (this.ids.length > 0) {
                    axios.get("/brand/delete?ids=" + this.ids).then(function (value) {
                        if (value.data) {
                            var page = (vue.checked && vue.page == vue.pages) ? vue.page - 1 : vue.page;
                            if (page <= 0) {
                                page = 1;
                            }
                            vue.search(page);
                        }
                    });
                } else {
                    alert("请选择要删除的品牌");
                }
            }
        },
        created: function () {
            this.search(this.page);
        },
        updated: function () {
            this.checked = (this.ids.length == this.dataList.length)
        }

    });
};